package com.open.capacity.forum.service.impl;

import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.util.PageUtil;
import com.open.capacity.common.web.CodeEnum;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.forum.constant.BusinessConstant;
import com.open.capacity.forum.constant.MetasConstant;
import com.open.capacity.forum.dao.CommentDao;
import com.open.capacity.forum.dao.CommentPraiseDao;
import com.open.capacity.forum.dto.CommentDto;
import com.open.capacity.forum.model.Comment;
import com.open.capacity.forum.model.CommentPraise;
import com.open.capacity.forum.service.CommentService;
import com.open.capacity.forum.util.Commons;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CommentServiceImpl extends ServiceImpl<CommentDao, Comment> implements CommentService {

    @Resource
    private CommentPraiseDao commentPraiseDao;

    @Override
    public PageResult findCommnet(Integer commentType, Map<String, Object> params) throws ServiceException {
        try {
            params.put(MetasConstant.COMMENT_TYPE, commentType);
            List<CommentDto> commentList = baseMapper.selectCommentByCondition(params);
            PageInfo<CommentDto> pageInfo = new PageInfo<>(Commons.fillCommentChildren(commentList));
            return PageResult.<CommentDto>builder().code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).data(pageInfo.getList()).build();
        } catch (Exception e) {
            log.warn(e.getMessage());
            throw new ServiceException(e);
        }
    }

    @Override
    @Transactional
    public Result commentSubmit(Integer CommentType, Comment comment) throws ServiceException {
        try {
            comment.setCotype(CommentType);
            boolean save = save(comment);
            if (save) {
                Map<String, Object> params = new HashMap<String, Object>() {{
                    put("comtid", comment.getComtid());
                }};
                CommentDto commentDto = baseMapper.selectCommentByCondition(params).get(0);
                return Result.succeed(commentDto, BusinessConstant.Default.succeed);
            }
            return Result.failed(BusinessConstant.Default.failed);
        } catch (Exception e) {
            log.warn(e.getMessage());
            throw new ServiceException(e);
        }
    }

    @Override
    @Transactional
    public Result commentPraise(CommentPraise commentPraise) throws ServiceException {
        try {
            boolean isChange;
            Long comtid = commentPraise.getComtid(), userid = commentPraise.getUserid();
            Integer praiseNum = getById(comtid).getPrenum();    // 当前评论的点赞数
            if (commentPraise.getPraiseStatus()) { // 当点赞状态为已点赞时
                isChange = commentPraiseDao.insert(commentPraise) > 0 && new LambdaUpdateChainWrapper<>(baseMapper).eq(Comment::getComtid, comtid).set(Comment::getPrenum, ++praiseNum).update();
            } else { // 取消点赞
                isChange = new LambdaUpdateChainWrapper<>(commentPraiseDao).eq(CommentPraise::getComtid, comtid).eq(CommentPraise::getUserid, userid).remove() && new LambdaUpdateChainWrapper<>(baseMapper).eq(Comment::getComtid, comtid).set(Comment::getPrenum, --praiseNum).update();
            }
            return isChange ? Result.succeed(praiseNum, BusinessConstant.Default.succeed) : Result.failed(BusinessConstant.Default.failed);
        } catch (Exception e) {
            log.warn(e.getMessage());
            throw new ServiceException(e);
        }
    }
}
